#include <iostream>

using namespace std;


int cache[9][9][2];


int max(int a, int b, int c, int d)
{
	int m1 = a>b?a:b;
	int m2 = c>d?c:d;
	return m1>m2?m1:m2;
}

int walk(int (&a)[9][9], int x, int y, int i, int j)
{
	if (x > i || y > j)
	{
		return 0;
	}
	
	if (cache[x][y][0] >= 0 && cache[x][y][1] >= 0)
	{
		return cache[x][y][0] + cache[x][y][1];
	}
	
	int rr[9][9], rd[9][9], dd[9][9], dr[9][9];
	memcpy(rr, a, 81*sizeof(int));
	memcpy(rd, a, 81*sizeof(int));
	memcpy(dr, a, 81*sizeof(int));
	memcpy(dd, a, 81*sizeof(int));

//	cout << x << "," << y << " : " << a << endl;
	
	int RR = walk(rr, x, y+1, i, j);
	RR += walk(rr, x, y+1, i, j);
	
	int RD = walk(rd, x, y+1, i, j);
	RD += walk(rd, x+1, y, i, j);
		
	int DR = walk(dr, x+1, y, i, j);
	DR += walk(dr, x, y+1, i, j);
	
	int DD = walk(dd, x+1, y, i, j); 
	DD += walk(dd, x+1, y, i, j);
	
	int t = (a[x][y]>=0?a[x][y]:0) + max(RR, RD, DR, DD);
	
	if (a[x][y] >= 0)
	{
		a[x][y] = -1;
		cache[x][y][0] = t;
	} 
	else
	{
		a[x][y] = -2;
		cache[x][y][1] = t;
	}
				
	return t;
}

int main(int argc, char *argv[]) 
{
	int n;
	int a[9][9];
	
	cin >> n;
	
	for (int i=0; i<n; ++i)
	{
		for (int j=0; j<n; ++j)
		{
			a[i][j] = 0;
		}
	}
	
	memset(cache, -1, 162*sizeof(int));
	
	int x, y, z;
	
	while (cin >> x >> y >> z)
	{
		if (x == 0 && y == 0 && z == 0)
		{
			break;
		}
		
		a[x-1][y-1] = z;
	}
	
	int c = walk(a, 0, 0, n-1, n-1);
	cout << c << endl;
	
	for (int i=0; i<n; ++i)
	{
		for (int j=0; j<n; ++j)
		{
			printf("%5d ", cache[i][j][0] + cache[i][j][1]);
		}
		cout << endl;
	}
	
	return 0;
}